<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>External programs</title>
<link href="tutor.css" title="Tutor Style" type="text/css" rel="stylesheet" />
<link href="tutor.css" type="text/css" rel="stylesheet" />
<link href="tutor_bw.css" title="Black and white"
type="text/css" rel="stylesheet" />
<link href="tutor_menu.css" title="Tutor Style" type="text/css" rel="stylesheet" />

</head>
<body>
<div id="topbar">
<img alt="" src="boing.gif" class="titleimage"/>
<span class="title">Popping windows</span>
</div>
<div id="navbar">
<span class="heading">Crunchy user tutorial</span>
<a href="index.html">Welcome</a>
<a href="interpreter.html">Interpreter</a>
<a href="editor.html">Editor</a>
<a href="doctest.html">DocTest</a>
<a href="canvas.html">Graphics: drawing</a>
<a href="plot.html">Graphics: plotting</a>
<a href="sound.html">Sound</a>
<a href="external.html">External applications</a>
</div>
<div id="main">
<h2>Or how to launch an external application</h2>
<p>One of the fun part of programming with Python is to produce your own programs running in a separate window. We'll give you a few examples to
try, ending with a simple Pygame script. 
Note that we know it is very likely that you might not be able to run some
of them if you don't have the necessary modules installed.  Click on "Execute externally" to launch them as separate processes; if nothing happens, click on "Evaluate" and see if there's an error message that appears.</p>
<p>The first example has to be a simple Tkinter application - if only because
Tkinter comes pretty much with all Python distributions and is likely
to be already present on your computer.<p>
<pre title="editor size=(6, 80) external">
from Tkinter import *
root = Tk()
w = Label(root, text="Crunchy!")
w.pack()
root.mainloop()
</pre>
<p>Our second example is a basic wxPython application.</p>
<pre title="editor size=(7, 80) external">
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Crunchy Test")
frame.SetBackgroundColour("#33cc33")
frame.SetSize(wx.Size(200, 100))
frame.Show(True)
app.MainLoop()
</pre>
<p>Our third example (shamelessly lifted from http://www.pygtk.org/pygtk2tutorial/), requires pygtk:</p>
<pre title="editor external">
import gtk
class HelloWorld:
    def hello(self, widget, data=None):
        print "Hello World"
    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False
    def destroy(self, widget, data=None):
        gtk.main_quit()
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.button = gtk.Button("Hello World")
        self.button.connect("clicked", self.hello, None)
        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
        self.window.add(self.button)
        self.button.show()
        self.window.show()
    def main(self):
        gtk.main()
hello = HelloWorld()
hello.main()
</pre>
<p>Our fourth, and final example, requires pygame.  If it works, you might
be interested in launching more than one!  Note that we used a VLAM option
so that it's only possible to launch it externally.</p>
<pre title="editor nointernal external">
import sys, pygame
pygame.init()
clock = pygame.time.Clock()
size = width, height = 320, 240
speed = [3, 3]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("small_boing.png")
ballrect = ball.get_rect()
while True:
    for event in pygame.event.get():
       if event.type == pygame.QUIT: sys.exit()
    ballrect = ballrect.move(speed)
    if ballrect.left &lt; 0 or ballrect.right &gt; width:
        speed[0] = -speed[0]
    if ballrect.top &lt; 0 or ballrect.bottom &gt; height:
        speed[1] = -speed[1]
    clock.tick(30)
    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()
</pre>
</div>
</body>
</html>